package com.huimingpay.merchant.service.impl;

import com.aliyun.oss.model.PutObjectRequest;
import com.huimingpay.common.domain.BusinessException;
import com.huimingpay.merchant.config.OssConfig;
import com.huimingpay.merchant.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * ClassName:FileServiceImpl
 * Package:com.huimingpay.merchant.service.impl
 * Time: 2024/7/22
 */

@Service
public class FileServiceImpl implements FileService {
    @Resource
    OssConfig ossConfig; //引入阿里云OSS客户端

    @Value("${oss.aliyun.bucket}")
    private String bucket;

    @Value("${oss.aliyun.domain}")
    private String domain;

    /**
     * 文件上传
     *
     * @param fileName    文件名称
     * @param inputStream 文件流
     * @return 文件路径(绝对url)
     * @throws BusinessException
     */
    @Override
    public String upload(String fileName, InputStream inputStream) throws BusinessException {

        //构建请求对象
        PutObjectRequest objectRequest = new PutObjectRequest(bucket, fileName, inputStream);
        //执行请求 文件上传
        ossConfig.getOssClient().putObject(objectRequest);

        //返回文件路径
        return domain + fileName;
    }
}
